<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="success" @click="handleCreate">&emsp;新增&emsp;</el-button>

        </el-row>
      </div>
      <div v-loading="listLoading" class="item">
        <el-table ref="dataList" :data="menuList" :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                  border row-key="id"
                  style="width:100%;margin-bottom: 20px;"
                  @selection-change="handleSelectionChange"
        >
          <!--          <el-table-column type="selection"></el-table-column>-->
          <el-table-column label="ID" prop="id"></el-table-column>
          <el-table-column label="分类" prop="category_name"></el-table-column>
          <el-table-column label="标签内容" prop="name"></el-table-column>

          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button icon="el-icon-edit" plain size="mini" type="primary" @click="handleUpdate(scope.row)">编辑
              </el-button>
              <el-button icon="el-icon-delete" plain size="mini" type="danger" @click="handleDelete(scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <!--        <cus-pagination-->
        <!--          :limit.sync="listQuery.perPage"-->
        <!--          :page.sync="listQuery.page"-->
        <!--          :total="total"-->
        <!--          @pagination="getList"-->
        <!--        />-->
      </div>
    </el-card>
    <!-- 删除弹框 -->
    <el-dialog
      :visible.sync="deleteDialogVisible"
      title="确定删除吗?"
      width="435px"
    >
      <span style=""><em style="font-style:normal;color:red">删除后不可恢复</em>，你还要继续吗？</span>
      <span slot="footer" class="dialog-footer">
            <el-button @click="deleteDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="confirmDelete">确 定</el-button>
        </span>
    </el-dialog>
    <!-- 新增编辑弹框 -->
    <el-dialog v-dialogDrag :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible" width="535px"
               @close="handleDialogClose('editRulesForm')"
    >
      <el-form ref="editRulesForm" :model="editRulesForm" :rules="editRulesFormRules" class="demo-ruleForm"
               label-width="120px"
      >

        <el-form-item label="标签分类" prop="category_id">
          <el-select v-model="editRulesForm.category_id" clearable filterable placeholder="请选择">
            <el-option v-for="item in selectOptions"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="标签内容" prop="name">
          <el-input v-model="editRulesForm.name" maxlength="10" placeholder="请输入标签名称" show-word-limit></el-input>
        </el-form-item>

        <el-form-item>
          <el-button @click="dialogFormVisible = false">返回</el-button>
          <el-button type="primary" @click="submitForm('editRulesForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listData } from '@/api/member/labelList'
import { listDataClassIf } from '@/api/member/labelClassif'
import { addLabel, delLabel, editLabel } from '../../api/member/labelList'

export default {
  data() {
    return {

      confirmBtn: false,

      specialFlagBigList: [],

      // 要修改的当前行的id
      row_id: 0,
      // 黑名单类别
      blackList: [
        { value: 1, label: '1' },
        { value: 2, label: '4' },
        { value: 3, label: '6' },
        { value: 4, label: '3' },
        { value: 5, label: '5' }
      ],
      // 选中的黑名单类别
      checkBlackList: [],
      provinceList: [],
      cityList: [],
      // 表单规则
      editRulesFormRules: {
        name: [{ required: true, message: '请输入标签内容', trigger: 'blur' }],
        category_id: [{ required: true, message: '请选择分类', trigger: 'change' }]
      },

      editRulesForm: {
        name: undefined,
        category_id: undefined
      },
      dialogStatus: '',
      // dialog的头部信息
      titleMap: {
        update: '编辑',
        create: '添加'
      },
      dialogFormVisible: false,
      deleteDialogVisible: false,
      listLoading: false,
      menuList: [],
      limitList: [],
      listQuery: {
        page: 1,
        perPage: 10
      },
      total: 0,
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: '',
      selectOptions: []
    }
  },
  created() {
    this.getList()
    this.getSelect()
  },
  methods: {
    getSelect() {
      listDataClassIf().then((response) => {
        this.selectOptions = response.data.map((item) => {
          item.label = item.name
          item.value = item.id
          return item
        })
      })
    },

    handleSpecialFlagBigSuccess() {
      this.editRulesForm.label_icon = this.ossLastImgUrl
      this.specialFlagBigList = [{ url: this.ossLastImgUrl }]
    },
    handleSpecialFlagBigRemove(res) {
      this.specialFlagBigList = []
    },
    getList() {
      this.listLoading = true
      listData().then((response) => {
        this.menuList = response.data
        this.listLoading = false
        this.confirmBtn = false
      })
    },
    // 点击新增
    handleCreate() {
      this.resetForm('editRulesForm')
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    // 点击编辑
    handleUpdate(row) {
      // row)
      this.row_id = row.id
      this.editRulesForm.name = row.name

      this.editRulesForm.category_id = row.category_id
      // this.specialFlagBigList = [{ url: row.label_icon }]

      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    // 重置表单内容
    resetForm(formName) {
      this.$nextTick(() => {
        // this.$refs[formName].resetFields();
        if (this.$refs[formName] !== undefined) {
          this.$refs[formName].resetFields()
        }
        this.editRulesForm = {
          label_name: undefined,
          label_icon: undefined,
          type: 2
        }
      })

      this.specialFlagBigList = []

    },
    // 勾选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    submitForm(formName) {
      if (this.confirmBtn) return

      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.confirmBtn = true
          // alert('submit!');
          if (this.dialogStatus === 'create') {
            // this.editRulesForm)
            addLabel(this.editRulesForm).then(response => {
              if (response.code === 200) {
                this.getList()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              } else {
                this.confirmBtn = false
              }
            })
          } else {
            editLabel(this.row_id, this.editRulesForm).then(response => {
              if (response.code === 200) {
                this.getList()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              } else {
                this.confirmBtn = false
              }
            })
          }
        } else {
          return false
        }
      })
    },
    // 点击单个删除
    handleDelete(row) {
      this.dataIds = row.id
      this.deleteDialogVisible = true
    },
    // 确定删除
    confirmDelete() {
      if (this.confirmBtn) return
      this.confirmBtn = true
      // this.dataIds)
      delLabel(this.dataIds).then((response) => {
        if (response.code === 200) {
          this.getList()
          this.deleteDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        } else {
          this.confirmBtn = false
        }
      })
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate() // 清除整个表单的校验
      }
    }
  }
}
</script>

<style scoped>
/deep/ .el-upload-list__item {
  transition: none !important;
}

/deep/ .el-upload-list--picture {
  max-height: 103px !important;
  overflow: hidden;
}
</style>
